# SuperFastPython.com
# example of issuing a task with apply_async() to the process pool with an error callback
from random import random
from time import sleep
from multiprocessing.pool import Pool
 
# handle any errors in the task function
def custom_error_callback(error):
    print(f'Got an Error: {error}', flush=True)
 
# task executed in a worker process
def task():
    # report a message
    print(f'Task executing', flush=True)
    # block for a moment
    sleep(1)
    # raise an exception
    raise Exception('Something bad happened')
    # report a message
    print(f'Task done', flush=True)
 
# protect the entry point
if __name__ == '__main__':
    # create and configure the process pool
    pool = Pool()
    for i in range(5):
        # issue tasks to the process pool
        pool.apply_async(task, error_callback=custom_error_callback)
    # close the process pool
    pool.close()
    # wait for all tasks to finish
    pool.join()